package com.futvan.z.erp.customer;

import com.futvan.z.erp.zstorehouse.ZstorehouseService;
import com.futvan.z.erp.zstorehouse.z_storehouse_area;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.core.SuperService;
import com.futvan.z.framework.core.z;
import com.futvan.z.system.zarea.z_area;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CustomerService extends SuperService {

    @Resource
    private ZstorehouseService zstorehouseService;

    /**
     * 根据zid获取用户信息
     *
     * @param zid
     * @return
     */
    public crm_customer getCustomerInfo(String zid) {

        crm_customer info = sqlSession.selectOne("crm_customer_select_zid", zid);
        return info;
    }


    public crm_customer_address getAddress(String allinfo) {
        crm_customer_address address = sqlSession.selectOne("crm_customer_address_select_sql", "select * from crm_customer_address where allinfo = '" + allinfo + "'");
        if(z.isNotNull(address)){
            z_area ar = z.areas.get(address.getAreaid());
            address.setObj(ar);
        }
        return address;
    }


    /**
     * 根据地址code获取地址信息
     *
     * @param addressid
     * @return
     */
    public crm_customer_address getCustomAddressByAddressid(String addressid) {
        crm_customer_address ca = null;

        List<crm_customer_address> address = sqlSession.selectList("crm_customer_address_select_zid", addressid);

        if (z.isNotNull(address) && address.size() == 1) {
            ca = address.get(0);
            z_area ar = z.areas.get(ca.getAreaid());
            ca.setObj(ar);
        }
        return ca;
    }

    /**
     * 根据客户ID获取地址
     *
     * @return
     */
    public crm_customer_address getCustomerAddress(String customerId, String addressid) {
        crm_customer_address ca = null;

        List<crm_customer_address> address = sqlSession.selectList("crm_customer_address_select_zid", "select * from crm_customer_address where pid = '" + customerId + "' and zid = '" + addressid + "'");

        if (z.isNotNull(address) && address.size() == 1) {
            ca = address.get(0);
            z_area ar = z.areas.get(ca.getAreaid());
            ca.setObj(ar);
        }
        return ca;
    }


    /**
     * 根据客户ID获取默认地址
     *
     * @param customid
     * @return
     */
    public crm_customer_address getCustomerDefaultAddress(String customid) {
        crm_customer_address ca = null;

        List<crm_customer_address> address = sqlSession.selectList("crm_customer_address_select_pid", customid);

        if (z.isNotNull(address) && address.size() > 0) {
            ca = address.get(0);
            z_area ar = z.areas.get(ca.getAreaid());
            ca.setObj(ar);
        }
        return ca;
    }


    /**
     * 根据地址ID获取地址
     *
     * @return
     */
    public crm_customer_address getCustomerAddressForAddressid(String addressid) {
        crm_customer_address ca = null;

        List<crm_customer_address> address = sqlSession.selectList("crm_customer_address_select_zid", addressid);

        if (z.isNotNull(address) && address.size() == 1) {
            ca = address.get(0);
            z_area ar = z.areas.get(ca.getAreaid());
            ca.setObj(ar);
        }
        return ca;
    }


    /**
     * 根据收货地址，获取发货仓库
     *
     * @return
     */
    public String getStartStorehouse(String addressid) {
        String default_storehouse_id = "";
        //判读收货ID是否为空
        if (z.isNotNull(addressid)) {
            crm_customer_address address = getCustomAddressByAddressid(addressid);
            //判读收货ID是否有效
            if (z.isNotNull(address)) {
                //获取收货地址对应省份
                String areaid = address.getAreaid().substring(0, 2) + "0000";
                //根据省份找到对应发货仓库ID
                z_storehouse_area sa = zstorehouseService.getStoreArea(areaid);
                if (z.isNotNull(sa)) {
                    default_storehouse_id = sa.getPid();
                } else {
                    default_storehouse_id = zstorehouseService.getString();
                    ;
                }
            } else {
                default_storehouse_id = zstorehouseService.getString();
                ;
            }
        } else {
            default_storehouse_id = zstorehouseService.getString();
            ;
        }
        return default_storehouse_id;
    }
}
